﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp
/// <summary>
/// 版 本 钢易智企2.0 V 2.0.0 钢易智企2.0
/// Copyright (c) 2023 杭州钢易科技有限公司
/// 创建人：曾东明
/// 日 期：2024
/// 描 述：
/// </summary>
{
	internal class DynamicFieldExample
	{
		public static Type AddFieldToType(Type type, string fieldName)
		{
			var assemblyName = new AssemblyName("DynamicAssembly");
			AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
			ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MainModule");
			TypeBuilder typeBuilder = moduleBuilder.DefineType(type.FullName, TypeAttributes.Public, type);

			Type[]  types= new Type[1] {typeof(int)};

			PropertyBuilder fieldBuilder = typeBuilder.DefineProperty(fieldName, PropertyAttributes.None,typeof(int), types);

            Type createType= typeBuilder.CreateType();
			Console.WriteLine(createType.Equals(type));
			Console.WriteLine(createType.FullName);
			Console.WriteLine(type.FullName);
			Object o=Activator.CreateInstance(createType);
			return createType;
        }
	}
}
